package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.intefaces.NewsItemCliclListener;
import com.zzk.lanouhn.myapplication1.modles.MessageArticles;

import java.util.List;

/**
 * Created by lanouhn on 16/9/1.
 */
public class MessageArticlesAdapter extends RecyclerView.Adapter<MessageArticlesAdapter.ViewHolder> {

    private Context context;
    private List<MessageArticles.DataBean.TopicBean> articles;
    private NewsItemCliclListener newsItemCliclListener;

    public void setScrollRecyclerViewAdapter(NewsItemCliclListener newsItemCliclListener) {
        this.newsItemCliclListener = newsItemCliclListener;
    }

    public MessageArticlesAdapter(Context context, List<MessageArticles.DataBean.TopicBean> articles) {
        this.context = context;
        this.articles = articles;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_articles_message,null);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        MessageArticles.DataBean.TopicBean bean=articles.get(position);
        holder.tv_title_message.setText(bean.getTitle());
        holder.tv_views_message.setText("浏览 "+bean.getViews());
        holder.tv_comments_message.setText("评论 "+bean.getComments());
        holder.tv_name_message.setText(bean.getUser().getNickname());
        holder.tv_time_message.setText(bean.getOrder_time_str());
        Picasso.with(context).load(bean.getUser().getAvatar()).into(holder.iv_icon_author_message);
        Picasso.with(context).load(bean.getPics().get(0).getUrl()).into(holder.iv_content_message);
        if (newsItemCliclListener!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos=holder.getLayoutPosition();
                    newsItemCliclListener.onItemClick(pos);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return articles.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_title_message, tv_views_message, tv_comments_message, tv_name_message, tv_time_message;
        private ImageView iv_content_message, iv_icon_author_message;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title_message = (TextView) itemView.findViewById(R.id.tv_title_acticles_message);
            tv_time_message = (TextView) itemView.findViewById(R.id.tv_time_articles_item);
            tv_views_message = (TextView) itemView.findViewById(R.id.tv_views_articles_item);
            tv_comments_message = (TextView) itemView.findViewById(R.id.tv_comments_articles_item);
            tv_name_message = (TextView) itemView.findViewById(R.id.tv_author_name_articles_item);
            iv_content_message = (ImageView) itemView.findViewById(R.id.iv_icon_content_acticles);
            iv_icon_author_message = (ImageView) itemView.findViewById(R.id.iv_author_icon_articles_item);

        }
    }
}
